package com.sunwayhorizo.health.hl7.rim;

import com.sunwayhorizo.health.common.*;
import com.sunwayhorizo.health.hl7.common.*;
import com.sunwayhorizo.health.hl7.datatypes.*;
import com.sunwayhorizo.health.hl7.datatypes.common.ADXPImpl;
import com.sunwayhorizo.health.hl7.factories.*;
import com.sunwayhorizo.health.hl7.persistent.*;
import java.io.*;
import java.util.*;

import javax.persistence.*;
import org.jboss.seam.annotations.*;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.*;

@Name("Organization")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("ORG")
public class Organization extends Hl7Entity implements Serializable
{

    /*
     * public BAG_AD addr; public CE standardIndustryClassCode;
     */

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    public Organization() throws Hl7Exception
    {
    }


    public Organization(ReferenceModifier referencemodifier)
    {
        super(referencemodifier);
    }


    // 新增ORG地址
    private void newAddr(AD addr)
    {
        RimCoreEntyAddrs entyAddrs = new RimCoreEntyAddrs();
        entyAddrs.setFormatted(addr.formatted().toString());
        entyAddrs.setEntyAddrUses(addr.use().toSet());
        entyAddrs.setEntyAddrParts(addr.toList());
        this.addEntyAddrs(entyAddrs);
    }


    /**
     * 设置ORG的地址，保存和修改地址都用此方法
     * 
     * @param addr
     *            ORG地址信息
     */
    @SuppressWarnings("rawtypes")
    public void setAddr(AD addr)
    {
        List mList = addr.toList();
        List<RimCoreEntyAddrs> addrList = this.getEntyAddrsList();
        if (ListUtil.isNotEmpty(addrList))
        {
            for (int i = 0; i < addrList.size(); i++)
            {
                RimCoreEntyAddrs addrs = addrList.get(i);
                String addrFormat = "";
                List<RimCoreEntyAddrParts> addrParts = addrs.getEntyeAddrPartsList();
                if (ListUtil.isNotEmpty(addrParts))
                {
                    for (int j = 0; j < addrParts.size(); j++)
                    {
                        RimCoreEntyAddrParts addPart = addrParts.get(j);
                        for (int k = 0; k < mList.size(); k++)
                        {
                            ADXPImpl ad = (ADXPImpl) mList.get(k);
                            if (addPart.getPartType().equals(ad.partType().code().toString()))
                            {
                                addrFormat += ad.literal().toString();
                                addPart.setLiteral(ad.literal().toString());
                                addPart.setPartOrderNum((long) k);
                            }
                        }
                    }
                }
                addrs.setFormatted(addrFormat);
            }
        }
        else
        {
            newAddr(addr);
        }
    }


    /**
     * 得到ORG的地址信息
     * 
     * @return
     * @throws Hl7Exception
     */
    @SuppressWarnings("rawtypes")
    public AD getAddr() throws Hl7Exception
    {

        List<RimCoreEntyAddrs> addsList = this.getEntyAddrsList();
        AD ad = null;
        if (addsList != null && !addsList.isEmpty())
        {
            RimCoreEntyAddrs entyAddrs = (RimCoreEntyAddrs) addsList.get(0);
            List mList = entyAddrs.getEntyeAddrPartsList();
            int size = mList.size();
            String value[] = new String[size];
            String type[] = new String[size];
            for (int i = 0; i < size; i++)
            {
                RimCoreEntyAddrParts partAddr = (RimCoreEntyAddrParts) mList.get(i);
                value[i] = partAddr.getLiteral();
                type[i] = partAddr.getPartType();
            }
            RimCoreEntyAddrUses addrUses = (RimCoreEntyAddrUses) entyAddrs.getEntyAddrUsesList().get(0);
            String useCode = addrUses.getUse();
            ad = DataTypeUtil.getAD(value, type, useCode);
        }

        return ad;
    }


    public void setStandardIndustryClassCode(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, STANDARDINDUSTRYCLASSCODE);
        }
        else
        {
            this.setOgStdIndClassCodeEtsId(ce.code().toString());
        }
    }


    public CE getStandardIndustryClassCode() throws Hl7Exception
    {
        if (this.getOgStdIndClassCodeEtsId().equals(STANDARDINDUSTRYCLASSCODE.toString()))
        {
            for (RimCoreEntyCdSets entityCdSets : this.getEntyCdSetsList())
            {
                if (this.getOgStdIndClassCodeEtsId().equals(entityCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entityCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getOgStdIndClassCodeEtsId());
        return null;
    }
    
    /**
     * 设置机构联系人
     * @param orgContact 机构联系人
     * @throws Hl7Exception
     */
    public void setOrgContact(ST orgContact) throws Hl7Exception
    {
        this.setOgContact((orgContact != null ? orgContact.stringValue() : ""));
    }
    
    /**
     * 得到机构联系人
     * @return ST
     * @throws Hl7Exception
     */
    public ST getOrgContact() throws Hl7Exception
    {
        return DataTypeUtil.getST(this.getOgContact());
    }
    

    @Column(name = "OG_CONTACT")
    private String ogContact; // 组织联系人

    @Column(name = "OG_LOCATION")
    private String ogLocation; // 位置

    @Column(name = "OG_STD_IND_CLASS_CODE_ETS_ID")
    private String ogStdIndClassCodeEtsId = STANDARDINDUSTRYCLASSCODE.toString();


    private void setOgStdIndClassCodeEtsId(String ogStdIndClassCodeEtsId)
    {
        this.ogStdIndClassCodeEtsId = ogStdIndClassCodeEtsId;
    }


    private String getOgStdIndClassCodeEtsId()
    {
        return ogStdIndClassCodeEtsId;
    }


    private String getOgContact()
    {
        return ogContact;
    }


    private void setOgContact(String ogContact)
    {
        this.ogContact = ogContact;
    }


    public String getOgLocation()
    {
        return ogLocation;
    }


    public void setOgLocation(String ogLocation)
    {
        this.ogLocation = ogLocation;
    }

}
